<template>
  <div class="login_container">
    <div class="login_box">
      <h2 class="login_title">简易教务管理系统</h2>
      <!-- 登录表单区域 -->
      <el-form class="login_form" :model="loginForm" :rules="loginRules" ref="loginForm">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input placeholder="请输入账号" v-model="loginForm.username">
            <i slot="prefix" class="el-input__icon el-icon-user"></i>
          </el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="loginForm.password" show-password>
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <!-- 身份 -->
        <el-form-item class="btns">
          <el-radio-group v-model="loginForm.identity">
            <el-radio label="teacher">教师</el-radio>
            <el-radio label="student">学生</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button type="info" @click="resetLoginForm">重置</el-button>
          <el-button type="primary" @click="onLogin">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginAPI } from '@/api/userAPI.js'

export default {
  name: 'Login',
  data () {
    return {
      // 登录用户信息
      loginForm: {
        username: '',
        password: '',
        identity: ''
      },
      // 表单验证规则
      loginRules: {
        username: [
          { required: true, msg: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, msg: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置表单
    resetLoginForm () {
      this.$refs.loginForm.resetFields()
    },
    // 登录
    onLogin () {
      // validate 对表单输入内容进行验证
      this.$refs.loginForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await loginAPI(this.loginForm)
        if (res.status !== 200) {
          this.$message.error(res.data.msg)
          return
        }
        this.$message.success(res.data.msg)
        // 将服务器传来的 token保存到 sessionStorage中
        sessionStorage.setItem('token', JSON.stringify({
          username: this.loginForm.username,
          identity: this.loginForm.identity
        }))
        // 跳转后台主页
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang='less' scoped>
  .login_container {
    height: 100%;
    background-color: aquamarine;
    position: relative;

    .login_box {
      width: 450px;
      height: 350px;
      background-color: #fff;
      border-radius: 5px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .login_title {
        text-align: center;
      }

      .login_form {
        box-sizing: border-box;
        width: 100%;
        padding: 0 20px;

        .btns {
          display: flex;
          justify-content: flex-end;
        }
      }
    }
  }
</style>
